package com.bmob.lover.ui.activity

import android.content.Intent
import android.os.Bundle
import android.view.View
import androidx.activity.viewModels
import com.hjq.toast.ToastUtils
import com.qmuiteam.qmui.arch.QMUILatestVisit
import com.bmob.lover.app.util.CacheUtil
import com.bmob.lover.data.constant.DEFAULT_LAUNCHER_BG
import com.bmob.lover.databinding.ActivityLauncherBinding
import com.bmob.lover.viewmodel.AppViewModel
import com.bmob.lover.viewmodel.LauncherViewModel
import com.bmob.lover.viewmodel.request.LoverBindRequestViewModel
import com.bmob.lover.viewmodel.request.UserRequestViewModel
import com.theone.common.ext.notNull
import com.theone.mvvm.core.base.activity.BaseCoreActivity
import com.theone.mvvm.ext.getAppViewModel

class LauncherActivity : BaseCoreActivity<LauncherViewModel, ActivityLauncherBinding>() {

    private val mUserVm: UserRequestViewModel by viewModels()
    private val mBindVm: LoverBindRequestViewModel by viewModels()
    private val mAppVm: AppViewModel by lazy { getAppViewModel<AppViewModel>() }

    override fun onCreate(savedInstanceState: Bundle?) {
        if (intent.flags and Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT != 0) {
            finish()
            return
        }
        super.onCreate(savedInstanceState)
    }

    override fun showTopBar(): Boolean = false

    override fun initView(root: View) {

    }

    override fun createObserver() {
        getViewModel().getUserLiveData().observe(this) {
            it.notNull({ user ->
                if (CacheUtil.getSessionToken().isNullOrEmpty()) {
                    ToastUtils.show("用户身份过期，请重新登录")
                    startActivity(LoginRegisterActivity::class.java)
                } else {
                    val launchBg =
                        if (user.launchBg.isNullOrEmpty()) DEFAULT_LAUNCHER_BG else user.launchBg
                    getViewModel().getFestivalBg()
                    getViewModel().launcherBg.set(launchBg)
                    mUserVm.requestServer()
                    mBindVm.mUserId = user.objectId
                    mBindVm.requestServer()
                    startActivityDelay()
                }
            }, {
                startActivity(LoginRegisterActivity::class.java)
            }
            )
        }
        mUserVm.getResponseLiveData().observe(this, androidx.lifecycle.Observer {
            mAppVm.setUserInfo(it)
        })
        mBindVm.getResponseLiveData().observe(this, androidx.lifecycle.Observer {
            if (it.isNotEmpty()) {
                it[0].let { bind ->
                    mAppVm.setLoverBind(bind)
                }
            }
        })
    }

    private fun startActivityDelay(to: Class<*>? = IndexActivity::class.java, delay: Long = 3000) {
        getContentView().postDelayed({
            startActivity(to)
        }, delay)
    }

    private fun startActivity(to: Class<*>? = IndexActivity::class.java) {
        var intent = QMUILatestVisit.intentOfLatestVisit(this)
        if (intent == null) {
            intent = Intent(this, to)
        }
        startActivity(intent)
        finish()
    }

}